﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using vngoodlife.Util;
using vngoodlife.Singleton;
using System.Text.RegularExpressions;

namespace vngoodlife.Controllers
{
    public class CommentController : Controller
    {
        //
        // GET: /Comment/

        #region BackEnd


        [CheckMemberVisibility, Authorize(Roles = ConstantClass.articleAuthentication)]
        public ActionResult CommentList_BackEnd(string commentTypeID = "1", int commentID = 0, string flag = "", string amountOfComment = "50", 
            string isBadOrPosted = "True")
        {
            
            ViewBag.CommentType = new SelectList(DataSingleton.Instance.GetCommentModels().GetCommentType(), "CommentTypeID", "CommentTypeName");
            ViewBag.CommentTypeID = commentTypeID;


            if (flag == "View" && commentID != 0)
            {
                ViewBag.CommentDetails = DataSingleton.Instance.GetCommentModels().GetCommentDetails(commentID);
            }

            ViewBag.isBadOrPosted = new SelectList(new[] { "True", "False" }.Select(x => new { value = x, text = x }), "value", "text", isBadOrPosted);
            ViewBag.isBadOrPostedValue = isBadOrPosted;

            ViewBag.amountList = new SelectList(new[] { "50", "200", "500", "1500", "5000" }.Select(x => new { value = x, text = x }),"value", "text", amountOfComment);
            ViewBag.AmountOfComment = amountOfComment;

            return View("/Views/BackEnd/Comment/CommentList.cshtml", DataSingleton.Instance.GetCommentModels().GetCommentList(Convert.ToInt16(commentTypeID)
                , Convert.ToInt16(amountOfComment), Convert.ToBoolean(isBadOrPosted) ));
        }


        [CheckMemberVisibility, Authorize(Roles = ConstantClass.articleAuthentication), HttpPost, ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        public ActionResult ChangeIsBad(int commentID, int commentTypeID, int amountOfComment, string isBadOrPosted)
        {
            string memberName = HttpContext.User.Identity.Name;
            DataSingleton.Instance.GetCommentModels().ChangeIsBad(commentID, memberName);

            return RedirectToAction("CommentList_BackEnd", "Comment", new { commentTypeID = commentTypeID, amountOfComment = amountOfComment, isBadOrPosted = isBadOrPosted });
        }


        [CheckMemberVisibility, Authorize(Roles = ConstantClass.articleAuthentication), HttpPost, ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        public ActionResult ChangeIsPosted(int commentID, int commentTypeID, int amountOfComment, string isBadOrPosted)
        {
            string memberName = HttpContext.User.Identity.Name;
            DataSingleton.Instance.GetCommentModels().ChangeIsPosted(commentID, memberName);

            return RedirectToAction("CommentList_BackEnd", "Comment", new { commentTypeID = commentTypeID, amountOfComment = amountOfComment, isBadOrPosted = isBadOrPosted });
        }


        [CheckMemberVisibility, Authorize(Roles = ConstantClass.articleAuthentication), HttpPost, ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        public ActionResult DeleteComment(int commentID, string commentTypeID, string amountOfComment, string isBadOrPosted)
        {
            DataSingleton.Instance.GetCommentModels().DeleteComment(commentID);

            return RedirectToAction("CommentList_BackEnd", "Comment", new { commentTypeID = commentTypeID, amountOfComment = amountOfComment, isBadOrPosted = isBadOrPosted });
        }


        #endregion


        #region FrontEnd


        [ChildActionOnly]
        public ActionResult CommentCreate(int articleID)
        {
            ViewBag.CommentArticleID = articleID;
            return PartialView("/Views/FrontEnd/Comment/_CommentCreate.cshtml");
        }

        //[ChildActionOnly]
        [HttpPost, ValidateInput(false)]
        [ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        public ActionResult GuestCommentCreate(Comment comment)
        {
            if (ModelState.IsValid)
            {
                DataSingleton.Instance.GetCommentModels().CreateComment(comment, 2, null);
            }

            return RedirectToAction("ArticleDetails", "Article", new { articleSeoUrl = comment.Article.ArticleSeoUrl });
        }

        //[ChildActionOnly]
        [Authorize, HttpPost, ValidateInput(false)]
        [ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        public ActionResult MemberCommentCreate(Comment comment)
        {
            string memberName = HttpContext.User.Identity.Name;
            if (comment.CommentContent != null)
            {
                DataSingleton.Instance.GetCommentModels().CreateComment(comment, 1, memberName);
            }

            return RedirectToAction("ArticleDetails", "Article", new { articleSeoUrl = comment.Article.ArticleSeoUrl });
        }



        [Authorize(Roles = ConstantClass.articleAuthentication), HttpPost, ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        public ActionResult DeleteCommentOnFrontEnd(int commentID, string articleSeoUrl)
        {
            DataSingleton.Instance.GetCommentModels().DeleteComment(commentID);

            return RedirectToAction("ArticleDetails", "Article", new { articleSeoUrl = articleSeoUrl });
        }

        #endregion

    }
}
